package com.langsheng.lsintell.ui.activity;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

import com.langsheng.lsintell.R;
import com.langsheng.lsintell.ui.widget.LSWaitingDialog;
import com.langsheng.lsintell.utils.LSUtil;

public class LSBaseActivity extends AppCompatActivity {

    protected TextView titleName;
    protected TextView titleSubName;
    protected TextView titleBackBtn;
    protected TextView titleLeftFun;
    protected TextView titleRightFun1;
    protected TextView titleRightFun2;

    private LSWaitingDialog waitingDialog;
    protected Handler handler = new Handler();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT > 21) {
            Window window = getWindow();
            //取消设置透明状态栏,使 ContentView 内容不再覆盖状态栏
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //需要设置这个 flag 才能调用 setStatusBarColor 来设置状态栏颜色
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            //设置状态栏颜色
            window.setStatusBarColor(getResources().getColor(R.color.colorPrimary));
        }
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }

    protected void initTitleView(View titleView) {
        if (titleView == null) {
            return;
        }
        titleName = (TextView) titleView.findViewById(R.id.tv_title_name);
        titleSubName = (TextView) titleView.findViewById(R.id.tv_title_sub_name);
        titleBackBtn = (TextView) titleView.findViewById(R.id.tv_title_back);
        titleLeftFun = (TextView) titleView.findViewById(R.id.tv_title_left_fun);
        titleRightFun1 = (TextView) titleView.findViewById(R.id.tv_title_right_fun1);
        titleRightFun2 = (TextView) titleView.findViewById(R.id.tv_title_right_fun2);

        titleBackBtn.setText(R.string.im_titlebar_left_back);
        titleBackBtn.setVisibility(View.VISIBLE);
        titleBackBtn.setCompoundDrawables(LSUtil.getBoundedDrawable(this, R.drawable.bigant_arrow_left), null, null, null);
        titleBackBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
    }

    protected LSWaitingDialog getWaitingDialog() {
        if (waitingDialog == null) {
            waitingDialog = new LSWaitingDialog(this);
        }
        return waitingDialog;
    }

//    protected void initSearchBar(View view, final int type, final int mode) {
//        if (view == null) {
//            return;
//        }
//        searchView = (LinearLayout) view.findViewById(R.id.rl_search_view);
//        searchName = (TextView) view.findViewById(R.id.tv_search_name);
//        searchView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent = new Intent(LSBaseActivity.this, BASearchActivity.class);
//                intent.putExtra(BASearchActivity.SEARCH_TAG, type);
//                intent.putExtra(BASearchActivity.SEARCH_MODE, mode);
//                if (mode != BASearchActivity.SELECT_MODE_MULTI) {
//                    startActivity(intent);
//                }
//            }
//        });
//    }

//    private BroadcastReceiver loginStateBC = new BroadcastReceiver() {
//        @Override
//        public void onReceive(Context context, Intent intent) {
//            if (BAActions.ACTION_LOGOUT.equals(intent.getAction())) {
//                boolean isForceOut = intent.getBooleanExtra(BAActions.EXTRA_KEY_LOGOUT_IS_FORCE_OUT, false);
//                showLogoutAlert(isForceOut);
//            } else if (BAActions.ACTION_ON_PWD_CHANGE_SUCCESS.equals(intent.getAction())) {
//                showPwdModifiedAlert();
//            } else if (BAActions.ACTION_LOGIN_FAILED.equals(intent.getAction())) {
//                int error = intent.getIntExtra(BAActions.EXTRA_KEY_ERROR_CODE, 0);
//                if (error == 208) {
//                    showPwdModifiedAlert();
//                }
//            }
//        }
//    };

    @Override
    protected void onResume() {
        super.onResume();
        HideSoftInput();
    }

    private boolean isRegister = false;


    @Override
    protected void onStop() {
        super.onStop();
    }
//
//    public void showLogoutAlert(boolean isForceOut) {
//        new AlertDialog.Builder(this).setTitle(R.string.im_text_reminder).setMessage(isForceOut ? R.string.im_user_force_logout_hint : R.string.im_user_logout_hint)
//                .setCancelable(false)
//                .setPositiveButton(R.string.im_text_ensure, new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialogInterface, int i) {
//                        BALoginInfos.getInstance().userLogout();
//                        BAIM.getInstance().logout();
//                        Intent intent = new Intent(LSBaseActivity.this, BALoginActivity.class);
//                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
//                        startActivity(intent);
//                        finish();
//                    }
//                }).show();
//    }
//
//    public void showPwdModifiedAlert() {
//        new AlertDialog.Builder(this).setTitle(R.string.im_text_reminder).setMessage(R.string.im_password_change_success)
//                .setCancelable(false)
//                .setPositiveButton(R.string.im_text_ensure, new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialogInterface, int i) {
//                        BALoginInfos.getInstance().userLogout();
//                        BAIM.getInstance().logout();
//                        Intent intent = new Intent(LSBaseActivity.this, BALoginActivity.class);
//                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
//                        startActivity(intent);
//                        finish();
//                    }
//                }).show();
//    }

    /**
     * 隐藏输入法
     */
    protected void HideSoftInput() {
        if (getCurrentFocus() != null) {
            IBinder token = getCurrentFocus().getWindowToken();
            if (token != null) {
                InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                manager.hideSoftInputFromWindow(token, InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    }

}
